using UnityEngine;
using System.Collections;

/// \class  gkAbyssalGiantTitleAnimationLauncher
/// \brief  Launch the launch animation on player interaction
[AddComponentMenu("GK/AbyssalGiant/Title/gkAbyssalGiantTitleAnimationLauncher")]
public class gkAbyssalGiantTitleAnimationLauncher : MonoBehaviour 
{	
	/// Idle animation
	public gkTextureAnimation IdleAnimation;
	
	/// Launch animation
	public gkTextureAnimation LaunchAnimation;
	
	/// Is the launch animation started?
	bool m_bLaunchAnimationStarted;
	
	/// \brief Use this for initialization
	void Start () 
	{
		PlayIdleAnimation();
	}
	
	/// \brief  Called when the component is enabled
	void OnEnable() 
	{
		// Listen the end of the launch animation
		GetComponent<gkEventComponent>().AddEventHandler(gkAbyssalGiantTitleEvent.LaunchAnimationEnd, HandleLaunchAnimationEndEvent);
	}
	
	/// \brief  Called when the component is disabled
	void OnDisable() 
	{	
		// Listen the end of the launch animation
		GetComponent<gkEventComponent>().RemoveEventHandler(gkAbyssalGiantTitleEvent.LaunchAnimationEnd, HandleLaunchAnimationEndEvent);
	}
	
	/// \brief  Update is called once per frame
	void Update() 
	{
		if(Input.GetButtonDown("Select"))
		{
			if(m_bLaunchAnimationStarted == false)
			{
				m_bLaunchAnimationStarted = true;
				PlayLaunchAnimation();
			}
			else
			{
				Application.LoadLevel("PlayScene");	
			}
		}
	}
	
	/// \brief  Handle the launch animation end
	void HandleLaunchAnimationEndEvent(Object a_rEvent)
	{
		Application.LoadLevel("PlayScene");
	}
	
	/// \brief  Play the idle animation
	void PlayIdleAnimation() 
	{
		// Activate the Idle animation
		IdleAnimation.enabled = true;
		// Deactivate the Launch animation
		LaunchAnimation.enabled = false;
	}
	
	/// \brief  Play the idle animation
	void PlayLaunchAnimation() 
	{
		// Activate the Idle animation
		IdleAnimation.enabled = false;
		// Deactivate the Launch animation
		LaunchAnimation.enabled = true;
	}
}